<template>
  <el-dialog
    v-model="dialogFormVisible"
    :title="title"
    width="870"
    @close="close"
  >
    <el-form
      ref="formRef"
      inline
      label-width="165px"
      :model="form"
      :rules="rules"
    >
      <el-form-item label="path：" prop="path">
        <el-input v-model="form.path" />
      </el-form-item>
      <el-form-item label="name：" prop="name">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="component：" prop="component">
        <el-input v-model="form.component" />
      </el-form-item>
      <el-form-item :label="translate('模块编码') + '：'" prop="code">
        <el-input v-model="form.code" />
      </el-form-item>

      <el-form-item :label="translate('模块名称') + '：'" prop="fullName">
        <el-input v-model="form.fullName" />
      </el-form-item>
      <!-- <el-form-item prop="parentId">
            <el-input v-model="form.parentId" />
          </el-form-item> -->
      <el-form-item :label="translate('上级模块') + '：'" prop="fullName">
        <el-tree-select
          v-model="form.parentId"
          :check-strictly="true"
          :data="tree"
          :props="defaultProps"
          :render-after-expand="false"
          show-checkbox
        />
      </el-form-item>
      <el-form-item :label="translate('模块分类') + '：'" prop="category">
        <el-select v-model="form.category">
          <el-option
            v-for="item in moduleList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="translate('icon图标') + '：'">
        <el-input v-model="form.icon" />
      </el-form-item>
      <el-form-item :label="translate('链接目录') + '：'" prop="target">
        <el-select v-model="form.target">
          <el-option
            v-for="item in linkList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="translate('级别层次') + '：'" prop="level">
        <el-input v-model="form.level" />
      </el-form-item>
      <el-form-item :label="translate('显示顺序') + '：'" prop="sortCode">
        <el-input v-model="form.sortCode" />
      </el-form-item>
      <el-form-item :label="translate('访问地址') + '：'">
        <el-input v-model="form.location" />
      </el-form-item>
      <el-form-item :label="translate('菜单显示') + '：'">
        <el-switch
          v-model="form.showOrNot"
          :active-value="1"
          :inactive-value="0"
        />
      </el-form-item>
      <el-form-item :label="translate('说明') + '：'">
        <el-input
          v-model="form.remark"
          autosize
          placeholder="Please input"
          type="textarea"
        />
      </el-form-item>
      <el-form-item :label="translate('选项') + '：'">
        <el-checkbox-group v-model="form.checkList">
          <el-checkbox
            v-for="item in checkBoxs"
            :key="item.value"
            :label="item.value"
          >
            {{ item.label }}
          </el-checkbox>
        </el-checkbox-group>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">{{ translate('取消') }}</el-button>
      <el-button type="primary" @click="save">
        {{ translate('确定') }}
      </el-button>
    </template>
  </el-dialog>
</template>

<script>
  // import VabIconSelector from '@/plugins/VabIconSelector'
  import { doEdit, getmenuinfo } from '@/api/menuManagement'
  import { translate } from '@/i18n'
  export default defineComponent({
    name: 'MenuManagementEdit',
    props: {
      tree: {
        type: Array,
        default: () => [],
      },
    },
    // components: { VabIconSelector },
    emits: ['refresh-route'],
    setup(props, { emit }) {
      const $baseMessage = inject('$baseMessage')

      const state = reactive({
        formRef: null,
        params: {},
        defaultProps: {
          children: 'ChildNodes',
          label: 'text',
        },
        parentList: [],
        form: {
          meta: {},
          checkList: [],
          showOrNot: 1,
          enabled: 0,
        },
        rules: {
          path: [
            {
              required: true,
              trigger: 'blur',
              message: translate('请输入path'),
            },
          ],
          name: [{ required: true, trigger: 'blur', message: '请输入name' }],
          component: [
            {
              required: true,
              trigger: 'blur',
              message: translate('请输入component'),
            },
          ],
          code: [
            {
              required: true,
              trigger: 'blur',
              message: translate('请输入模块编码'),
            },
          ],
          fullName: [
            {
              required: true,
              trigger: 'blur',
              message: translate('请输入模块名称'),
            },
          ],
          parentId: [
            {
              required: true,
              trigger: 'blur',
              message: translate('请输入模块名称'),
            },
          ],
          category: [
            {
              required: true,
              trigger: 'change',
              message: translate('请选择模块分类'),
            },
          ],
          target: [
            {
              required: true,
              trigger: 'change',
              message: translate('请选择连接目录'),
            },
          ],
          level: [
            {
              required: true,
              trigger: 'blur',
              message: translate('请输入级别层次'),
            },
          ],
          sortCode: [
            {
              required: true,
              trigger: 'change',
              message: translate('请输入显示顺序'),
            },
          ],
        },
        title: '',
        dialogFormVisible: false,
        moduleList: [
          {
            value: '页面',
            label: '页面',
          },
          {
            value: '目录',
            label: '目录',
          },
        ],
        linkList: [
          {
            value: 'iframe',
            label: 'iframe',
          },
          {
            value: 'click',
            label: 'click',
          },
        ],
        checkBoxs: [
          { value: 'authority', label: translate('系统菜单') },
          { value: 'dataScope', label: translate('数据范围') },
          { value: 'allowView', label: translate('动态视图') },
          { value: 'allowButton', label: translate('动态按钮') },
          { value: 'allowForm', label: translate('动态表单') },
          { value: 'isexpand', label: translate('展开') },
          { value: 'enabled', label: translate('有效') },
        ],
      })

      const handleIcon = (item) => {
        state.form.meta.icon = item
      }
      //获取详情数据
      const getmenu = async (row) => {
        state.listLoading = true
        const {
          data: { data },
        } = await getmenuinfo(row)
        state.form = data
        if (!data.showOrNot && data.showOrNot == null) {
          state.form.showOrNot = 1
        }
        state.form.checkList = []
        if (data.authority === 1) {
          state.form.checkList.push('authority')
        }
        if (data.dataScope === 1) {
          state.form.checkList.push('dataScope')
        }
        if (data.allowView === 1) {
          state.form.checkList.push('allowView')
        }
        if (data.allowButton === 1) {
          state.form.checkList.push('allowButton')
        }
        if (data.allowForm === 1) {
          state.form.checkList.push('allowForm')
        }
        if (data.isexpand === 1) {
          state.form.checkList.push('isexpand')
        }

        if (data.enabled === 1) {
          state.form.checkList.push('enabled')
        }
        state.listLoading = false
      }
      //编辑传值
      const showEdit = (row, params) => {
        state.parentList = params.list
        if (!row.ModuleId) {
          state.title = '添加'
          state.form.parentId = row
        } else {
          state.title = '编辑'
          getmenu({ menuId: row.ModuleId })
        }
        state.params = JSON.parse(JSON.stringify(params.queryForm))
        state.dialogFormVisible = true
      }
      //弹框关闭
      const close = () => {
        state['formRef'].resetFields()
        state.form = {
          meta: {
            icon: '',
          },
        }
        state.dialogFormVisible = false
      }
      //数据保存
      const save = async () => {
        console.log(1111, state.form)
        state.form.enabled = 0
        if (state.form.checkList && state.form.checkList.length > 0) {
          state.form.checkList.map((item) => {
            switch (item) {
              case 'authority':
                state.form.authority = 1
                break
              case 'dataScope':
                state.form.dataScope = 1
                break
              case 'allowView':
                state.form.allowView = 1
                break
              case 'allowButton':
                state.form.allowButton = 1
                break
              case 'allowForm':
                state.form.allowForm = 1
                break
              case 'isexpand':
                state.form.isexpand = 1
                break
              case 'enabled':
                state.form.enabled = 1
                break
            }
          })
        }
        state['formRef'].validate(async (valid) => {
          if (valid) {
            const { message } = await doEdit(state.form)
            $baseMessage(message, 'success', 'vab-hey-message-success')
            close()
            emit('refresh-route')
          }
        })
      }

      return {
        ...toRefs(state),
        handleIcon,
        showEdit,
        close,
        save,
        translate,
      }
    },
  })
</script>
<style scoped>
  .el-form {
    display: flex;
    flex-flow: wrap;
    justify-content: left;
  }
  .el-form >>> .el-input {
    width: 215px !important;
  }
  .el-form >>> .el-select {
    width: 215px !important;
  }
  .el-form-item {
    width: 380px;
  }
</style>
